package com.zhdl.modules.tools;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class ProcessTask implements Runnable {
    public boolean running = true;
    private int timeOut = 20;
    protected int current = 0;
    private  int interval = 100;
    public ProcessTask(int timeOut) {
        this.timeOut = timeOut;
    }
    public ProcessTask(int timeOut, int interval) {
        this.timeOut = timeOut;
        this.interval = interval;
    }

    private CommonCallback<String> timeOutCallBack;

    public void setTimeOutCallBack(CommonCallback<String> timeOutCallBack) {
        this.timeOutCallBack = timeOutCallBack;
    }

    @Override
    public void run() {
        while (running) {
            doTask();
            if (running && current <= 5 && current >= 2) {
                reTry();
            }
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (timeOut>0){
                current++;
                if (current >= timeOut && running) {
                    running = false;
                    timeOutCallBack.statusCallback("ok");
                    log.info("ProcessTask: 任务超时!!!!!!");
                }
            }
        }
    }

    protected abstract void doTask();

    protected void done() {
        running = false;
        log.info("当前任务执行完毕!!!");
    }
    protected void reTry(){

    }
}
